package by.grsu.vashchilo.page.administrator;

import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.vashchilo.app.AirlineWebApplication;
import by.grsu.vashchilo.dao.impl.TicketDao;
import by.grsu.vashchilo.dao.impl.TripDao;
import by.grsu.vashchilo.entity.Ticket;
import by.grsu.vashchilo.page.administrator.ListTicketPage;

public class TicketEditPage extends WebPage{
	public TicketEditPage(final Ticket ticket) {
		super();

		final Form<Ticket> form = new Form<Ticket>("form",
				new CompoundPropertyModel<Ticket>(ticket));
		add(form);

		form.add(new TextField<Long>("id"));
		final TripDao tripDao = new TripDao(
				AirlineWebApplication.DATA_XML_FOLDER);
		final List<Long> trip = tripDao.getAllID();
		final ListChoice listChoice = new ListChoice("tripid", trip);
		form.add(listChoice);
		form.add(new TextField<String>("seat"));
		
		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				final TicketDao ticketDao = new TicketDao(
						AirlineWebApplication.DATA_XML_FOLDER);
				if (ticket.getId() == null) {
					ticketDao.saveNew(ticket);
				} else {
					ticketDao.update(ticket);
				}
				setResponsePage(new ListTicketPage());
			}
		});

		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(new ListTicketPage());

			}
		});
	}

}
